using Engine.Graphics;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;

namespace Scenario_Editor.Controls
{
    public partial class MaterialMappingsMatrix : UserControl
    {
        #region Private Fields

        private Mappings mapping;
        private Dictionary<EnumMappingMode, Mappings> standardMapping;

        #endregion Private Fields

        #region Public Constructors

        /// <summary>
        /// Constructor.
        /// </summary>
        public MaterialMappingsMatrix()
        {
            InitializeComponent();
            mapping = new Mappings((float)this.numericUpDown_RotationX.Value, (float)this.numericUpDown_RotationY.Value, (float)this.numericUpDown_RotationZ.Value,
                                    (float)this.numericUpDown_ScaleX.Value, (float)this.numericUpDown_ScaleZ.Value);

            mapping.OffsetX = (float)this.numericUpDown_OffsetX.Value;
            mapping.OffsetY = (float)this.numericUpDown_OffsetZ.Value;

            // remove the button
            this.button1.Visible = false;

            SetUpStandardMatrixMappings();
        }

        #endregion Public Constructors

        #region Public Delegates

        public delegate void OnMaterialChange(object material, EventArgs e);

        #endregion Public Delegates

        #region Public Events

        public event OnMaterialChange materialChange;

        #endregion Public Events

        #region Public Properties

        public Mappings Mapping
        {
            get
            {
                return this.mapping;
            }
            set
            {
                Mappings mapping = value;
                if (mapping.RotationX < 0) mapping.RotationX += 360;
                if (mapping.RotationY < 0) mapping.RotationY += 360;
                if (mapping.RotationZ < 0) mapping.RotationZ += 360;

                this.numericUpDown_ScaleX.ValueChanged -= ValueChanged;
                this.numericUpDown_ScaleZ.ValueChanged -= ValueChanged;

                this.numericUpDown_OffsetX.Value = (decimal)mapping.OffsetX;
                this.numericUpDown_OffsetZ.Value = (decimal)mapping.OffsetY;
                this.numericUpDown_RotationX.Value = (decimal)mapping.RotationX;
                this.numericUpDown_RotationY.Value = (decimal)mapping.RotationY;
                this.numericUpDown_RotationZ.Value = (decimal)mapping.RotationZ;
                if (!float.IsInfinity(mapping.ScaleX))
                    this.numericUpDown_ScaleX.Value = (decimal)mapping.ScaleX;
                if (!float.IsInfinity(mapping.ScaleY))
                    this.numericUpDown_ScaleZ.Value = (decimal)mapping.ScaleY;

                this.numericUpDown_ScaleX.ValueChanged += new EventHandler(ValueChanged);
                this.numericUpDown_ScaleZ.ValueChanged += new EventHandler(ValueChanged);

                this.mapping = value;
            }
        }

        #endregion Public Properties

        #region Private Methods

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(this.mapping.mappings1.ToString() + "\n" +
                            this.mapping.mappings2.ToString());
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Mappings mappings;
            if (this.standardMapping.TryGetValue((EnumMappingMode)this.comboBox1.SelectedValue, out mappings))
            {
                this.Mapping = mappings;
            }
            else
                Debug.Write("No Standard value in MaterialMapping");
            if (this.materialChange != null)
                this.materialChange(this.mapping, null);
            SetUpStandardMatrixMappings();
        }

        private void ControlGetsFocus(object sender, EventArgs e)
        {
            if (sender is NumericUpDown)
            {
                NumericUpDown nud = sender as NumericUpDown;
                nud.Select(0, ((NumericUpDown)sender).Text.Length);
            }
        }

        private void SetUpStandardMatrixMappings()
        {
            this.standardMapping = new Dictionary<EnumMappingMode, Mappings>();
            this.standardMapping.Add(EnumMappingMode.material00, new Mappings(0, 0, 0, 100, 100));// Material00
            this.standardMapping.Add(EnumMappingMode.material02, new Mappings(0, 0, 0, 100, 100));// Material02
            this.standardMapping.Add(EnumMappingMode.material16, new Mappings(90, 0, 0, 100, 100));// Material16
            this.standardMapping.Add(EnumMappingMode.material32, new Mappings(0, 0, 0, 1, 1));// Material32
            this.standardMapping.Add(EnumMappingMode.material48, new Mappings(90, 0, 0, 100, 100));// Material48
            this.standardMapping.Add(EnumMappingMode.material64, new Mappings(0, 0, 0, 100, 100));// Material64
            this.standardMapping.Add(EnumMappingMode.material80, new Mappings(0, 0, 0, 100, 100));// Material80
            this.standardMapping.Add(EnumMappingMode.material96, new Mappings(90, 0, 0, 1, 1));// Material96
            this.standardMapping.Add(EnumMappingMode.material112, new Mappings(0, 0, 0, 100, 100));// Material112
            this.standardMapping.Add(EnumMappingMode.material128, new Mappings(0, 0, 0, 100, 100));// Material128
            this.standardMapping.Add(EnumMappingMode.material144, new Mappings(90, 0, 0, 1, 1));// Material144
            this.standardMapping.Add(EnumMappingMode.material160, new Mappings(0, 0, 0, 100, 100));// Material160
            this.standardMapping.Add(EnumMappingMode.material176, new Mappings(90, 0, 0, 1, 1));// Material176
            this.standardMapping.Add(EnumMappingMode.material192, new Mappings(0, 0, 0, 100, 100));// Material192
            this.standardMapping.Add(EnumMappingMode.material208, new Mappings(0, 0, 0, 100, 100));// Material208
            this.standardMapping.Add(EnumMappingMode.material224, new Mappings(0, 0, 0, 100, 100));// Material224
            this.standardMapping.Add(EnumMappingMode.material240, new Mappings(0, 0, 0, 100, 100));// Material240

            if (this.comboBox1.Items.Count == 0)
                this.comboBox1.DataSource = Enum.GetValues(typeof(EnumMappingMode));
        }

        private void ValueChanged(object sender, EventArgs e)
        {
            NumericUpDown control = (NumericUpDown)sender;
            if (control.Maximum == 360)// check if it is a numericUpdown Control which is used for angles
            {
                if (control.Value == control.Maximum)
                    control.Value = 0;
                if (control.Value == control.Minimum)
                    control.Value = 359;
            }

            mapping.OffsetX = (float)this.numericUpDown_OffsetX.Value;
            mapping.OffsetY = (float)this.numericUpDown_OffsetZ.Value;
            mapping.RotationX = (float)this.numericUpDown_RotationX.Value;
            mapping.RotationY = (float)this.numericUpDown_RotationY.Value;
            mapping.RotationZ = (float)this.numericUpDown_RotationZ.Value;
            mapping.ScaleX = (float)this.numericUpDown_ScaleX.Value;
            mapping.ScaleY = (float)this.numericUpDown_ScaleZ.Value;

            //Fire the event
            if (materialChange != null)
                materialChange(mapping, new EventArgs());
        }

        #endregion Private Methods
    }
}